<!DOCTYPE html>
<html>
<head>
    <title>机械臂控制</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .header { margin-bottom: 20px; }
        .control-panel { margin-bottom: 20px; }
        .btn {
            padding: 8px 16px;
            margin-right: 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn-start { background-color: #4CAF50; color: white; }
        .btn-pause { background-color: #2196F3; color: white; }
        .btn-resume { background-color: #FF9800; color: white; }
        .btn-stop { background-color: #f44336; color: white; }
        .position-panel {
            border: 1px solid #ddd;
            padding: 15px;
            border-radius: 4px;
        }
        .axis {
            display: flex;
            margin-bottom: 10px;
        }
        .axis-label {
            width: 30px;
            font-weight: bold;
            color: #666;
        }
        .axis-value {
            color: #2196F3;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h2>当前 IP：<span id="ip">{{ ip }}</span></h2>
    </div>
    
    <div class="control-panel">
        <button class="btn btn-start" onclick="control('start')">启动</button>
        <button class="btn btn-pause" onclick="control('pause')">暂停</button>
        <button class="btn btn-resume" onclick="control('resume')">继续</button>
        <button class="btn btn-stop" onclick="control('stop')">停止</button>
    </div>

    <div class="position-panel">
        <h3>机械臂末端位置</h3>
        <div class="axis">
            <span class="axis-label">X</span>
            <span class="axis-value" id="x-value">0.00</span>
        </div>
        <div class="axis">
            <span class="axis-label">Y</span>
            <span class="axis-value" id="y-value">0.00</span>
        </div>
        <div class="axis">
            <span class="axis-label">Z</span>
            <span class="axis-value" id="z-value">0.00</span>
        </div>
        <div class="axis">
            <span class="axis-label">R</span>
            <span class="axis-value" id="r-value">0.00</span>
        </div>
    </div>

    <script>
        // 定期更新位置信息
        function updatePosition() {
            fetch('/api/position')
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        console.error('获取位置失败:', data.error);
                        return;
                    }
                    document.getElementById('x-value').textContent = data.x.toFixed(2);
                    document.getElementById('y-value').textContent = data.y.toFixed(2);
                    document.getElementById('z-value').textContent = data.z.toFixed(2);
                    document.getElementById('r-value').textContent = data.a.toFixed(2);
                })
                .catch(error => console.error('请求失败:', error));
        }

        // 控制命令
        function control(cmd) {
            fetch(`/api/control/${cmd}`)
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        alert('错误: ' + data.error);
                    } else {
                        console.log('命令执行成功:', data.message);
                    }
                })
                .catch(error => {
                    console.error('请求失败:', error);
                    alert('操作失败');
                });
        }

        // 每秒更新一次位置
        setInterval(updatePosition, 1000);
        // 页面加载时立即更新一次位置
        updatePosition();
    </script>
</body>
</html>